/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import java.util.List;

import com.hboxs.asl.dao.AttributeDao;
import com.hboxs.asl.entity.Attribute;
import com.hboxs.asl.entity.ProductCategory;
import com.hboxs.asl.service.AttributeService;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service - 属性
 */
@Service("attributeServiceImpl")
public class AttributeServiceImpl extends BaseServiceImpl<Attribute, Long> implements AttributeService {
	@Resource(name = "attributeDaoImpl")
	private AttributeDao attributeDao;
	
	@Resource(name = "attributeDaoImpl")
	public void setBaseDao(AttributeDao attributeDao) {
		super.setBaseDao(attributeDao);
	}
	

	@Override
	@Transactional(readOnly = true)
	public JSONArray buildAttributeTree(ProductCategory pc){
		Attribute attribute = new Attribute();
		attribute.setProductCategory(pc);
		List<Attribute> aList = null;
		try{
			aList = attributeDao.findAttributeList(attribute);
		}catch(Exception e){
			e.printStackTrace();
		}
		/*构建树*/
		JSONArray array = new JSONArray();
		JSONObject tree = new JSONObject();
		tree.put("id", 1);
		tree.put("pId", 0);
		tree.put("name", "商品属性");
		tree.put("open", true);
		array.put(tree);
		for(Attribute a: aList){
			JSONObject child = new JSONObject();
			child.put("id", a.getId());
			child.put("pId", 1);
			child.put("name", a.getName());
			child.put("open", false);
			array.put(child);
		}
		return array;
		
	}
	
	@Override
	@Transactional(readOnly = true)
	public JSONArray buildAttributeOptionTree(Attribute a){
		List<String> optionList = a.getOptions() ;
		
		/*构建树*/
		JSONArray array = new JSONArray();
		if(optionList==null){return array;}
		JSONObject tree = new JSONObject();
		tree.put("id", 1);
		tree.put("pId", 0);
		tree.put("name", "商品属性参数");
		tree.put("open", true);
		array.put(tree);
		for(int i=0;i<optionList.size();i++){
			JSONObject child = new JSONObject();
			child.put("id", i+2);
			child.put("pId", 1);
			child.put("name", optionList.get(i));
			child.put("open", false);
			array.put(child);
		}
		return array;
		
	}
}